/*
 * Copyright 2012-2014 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jqd.examples.jpa2.feature02;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

/**
 * TODO
 * 
 * @author liaozhicheng.cn@gmail.com
 * @date 2012-9-1
 * @since 1.0
 */
@Entity
@Table(name = "EMPLOYEE")
@Access(AccessType.FIELD)
@DynamicInsert
@DynamicUpdate
class Employee {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(nullable = false, length = 50)
	private String name;
	
	private byte bytePro;
	
	@Lob
	@Basic(fetch = FetchType.LAZY)
	// byte[], Byte[], Serializable --> BLOB
	private byte[] byteArrayPro;
	
	@Lob
	// char[], Character[], String --> CLOB
	private char[] charArrayPro;
	
	// 数据库中存储的将是枚举字符串，默认保存的将是枚举类型的序号，即 EnumType.ORDINAL
	@Enumerated(EnumType.STRING)
	private EmployeeType employeeType;
	
	// java.util.Date
	@Temporal(TemporalType.DATE)
	private Date createDate;
	
	// java.sql.Timestamp
	private Timestamp timestamp;
	
	@Transient
	private String transientPro1;
	
	// 使用 @Transient 及 transient 关键字修饰的属性，将不会映射到数据库中对应表的列
	transient private String transientPro2;
	
	@Transient
	private String phoneNumber;
	
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	
	
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	
	@Access(AccessType.PROPERTY)
	@Column(name = "PHONE_NUMBER")
	protected String getPhoneNumberForDb() {
		return "0755" + phoneNumber;
	}
	
	
	protected void setPhoneNumberForDb(String phoneNumber) {
		if (phoneNumber.startsWith("0755")) {
			this.phoneNumber = phoneNumber.substring(4);
		}
		else {
			this.phoneNumber = phoneNumber;
		}
	}
	
	
	public Long getId() {
		return id;
	}
	
	
	public void setId(Long id) {
		this.id = id;
	}
	
	
	public String getName() {
		return name;
	}
	
	
	public void setName(String name) {
		this.name = name;
	}
	
	
	public byte getBytePro() {
		return bytePro;
	}
	
	
	public void setBytePro(byte bytePro) {
		this.bytePro = bytePro;
	}
	
	
	public byte[] getByteArrayPro() {
		return byteArrayPro;
	}
	
	
	public void setByteArrayPro(byte[] byteArrayPro) {
		this.byteArrayPro = byteArrayPro;
	}
	
	
	public char[] getCharArrayPro() {
		return charArrayPro;
	}
	
	
	public void setCharArrayPro(char[] charArrayPro) {
		this.charArrayPro = charArrayPro;
	}
	
	
	public EmployeeType getEmployeeType() {
		return employeeType;
	}
	
	
	public void setEmployeeType(EmployeeType employeeType) {
		this.employeeType = employeeType;
	}
	
	
	public Date getCreateDate() {
		return createDate;
	}
	
	
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	
	public Timestamp getTimestamp() {
		return timestamp;
	}
	
	
	public void setTimestamp(Timestamp timestamp) {
		this.timestamp = timestamp;
	}
	
	
	public String getTransientPro1() {
		return transientPro1;
	}
	
	
	public void setTransientPro1(String transientPro1) {
		this.transientPro1 = transientPro1;
	}
	
	
	public String getTransientPro2() {
		return transientPro2;
	}
	
	
	public void setTransientPro2(String transientPro2) {
		this.transientPro2 = transientPro2;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		}
		else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
